package xyz.xmh.pattern.structural.flyweightpattern;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Administrator
 * 形状工厂
 */
public class ShapeFactory {

    /**
     * 缓存圆形对象
     */
    private static final Map<String, Shape> SHAPE_MAP = new HashMap<>();

    /**
     * 根据颜色获取对应的圆形对象
     *
     * @param color 颜色
     * @return 对应的圆形对象
     */
    public static Shape getCircle(String color) {
        Circle circle = (Circle) SHAPE_MAP.get(color);

        if (circle == null) {
            circle = new Circle(color);
            SHAPE_MAP.put(color, circle);
            System.out.println("创建圆形对象，颜色为：" + color);
        }

        return circle;
    }


}
